﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace FBNotifier
{
    /// <summary>
    /// Interaction logic for FBAlertWindow.xaml
    /// </summary>
    public partial class FBAlertWindow : DesktopAlert.DesktopAlertBase
    {
        public static bool NotifierWindowOpened = false;

        public FBAlertWindow()
        {
            NotifierWindowOpened = true;
            
            InitializeComponent();
        }

        private bool LoadNewMessage()
        {
            FacebookEvent fbEvent = new FacebookEvent();

            if (NewEventsSystem.PopEvent(ref fbEvent))
            {
                if (fbEvent.eventType == FacebookEventTypes.Message)
                {
                    Title = "New message from " + fbEvent.authorName;
                }
                else if (fbEvent.eventType == FacebookEventTypes.Notification)
                {
                    Title = "New notification ...";
                }

                subject.Text = fbEvent.subject;
                message.Text = fbEvent.body;
                time.Content = FacebookAuxClass.ConvertFBTimestamp(double.Parse(fbEvent.timeReceived)).ToString();
                AlertURL = fbEvent.url;

                if (AlertURL != "")
                {
                    Cursor = Cursors.Hand;
                }
                    
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(fbEvent.photoUrl);
                image.EndInit();

                image1.Source = image;

                return true;
            }
            else
            {
                return false;
            }
        }

        override protected void DesktopAlertBase_Loaded(object sender, RoutedEventArgs e)
        {
            LoadNewMessage();
            
            base.DesktopAlertBase_Loaded(sender, e);
        }

        protected override void FadeOutAnimation_Completed(object sender, EventArgs e)
        {
            base.FadeOutAnimation_Completed(sender, e);
        }

        protected override void ActiveTimer_Completed(object sender, EventArgs e)
        {
            Debug.WriteLine("ActiveTimer_Completed");

            //ако имаме още съобщения зареждаме някое от тях и чакаме до следващото извикване на timer-a
            //иначе просто затвараме notifiction window-a
            if (!LoadNewMessage())
            {
                Debug.WriteLine("ActiveTimer_Completed >> queue is empty. Closing the window ...");
                NotifierWindowOpened = false;

                base.ActiveTimer_Completed(sender, e);
            }
        }
    }
}
